package sz.szsmk.citizencard.ui.dzsmkavtivity;

import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.entity.BillFilterBean;


/**
 * Created by hyt on 2019/06/20.
 */

public class BillFilterActivity extends AppBaseActivity{

    TagFlowLayout tfl_income;
    TagFlowLayout tfl_expenditure;
    TagFlowLayout tfl_transactionTime;

    TagAdapter incomeAdapter;
    TagAdapter expenditureAdapter;
    TagAdapter transactionAdapter;

    List<String> incomeType = Arrays.asList( new String[]{"充值","奖励金","结息","退款"});
    List<String> expenditureType = Arrays.asList( new String[]{"余额","奖励金","组合"});

    protected LayoutInflater mInflater;

    //界面关闭  返回的时间
    List<String> transactionTypeResult = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(this);
        setContentView(R.layout.qr_activity_billfilter);

        tfl_income = findViewById(R.id.tfl_income);

        tfl_expenditure = findViewById(R.id.tfl_expenditure);

        tfl_transactionTime = findViewById(R.id.tfl_transactionTime);


        findViewById(R.id.btn_reset).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                incomeAdapter.setSelectedList(new HashSet<>());
                expenditureAdapter.setSelectedList(new HashSet<>());
                transactionAdapter.setSelectedList(new HashSet<>());


                billFilterBean.setTradeDate("");
                billFilterBean.setInType("");
                billFilterBean.setOutType("");
            }
        });


        findViewById(R.id.btn_submit).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(billFilterBean);
                finish();
            }
        });



        initTagFlowLayout();
//        btn_reset();
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true, 0f)
                .statusBarColor(R.color.white).init();
    }


    @Override
    protected CharSequence getTopTitle() {
        return "筛选";
    }

    private void initTagFlowLayout(){
        mInflater = LayoutInflater.from(mContext);

        tfl_income.setAdapter(incomeAdapter = new  TagAdapter<String>(incomeType){

            @Override
            public View getView(FlowLayout parent, int position, String s){
                TextView tv = (TextView) mInflater.inflate(R.layout.item_tv_flow,tfl_income, false);
                tv.setText(s);
                return tv;
            }
        });

        tfl_expenditure.setAdapter(expenditureAdapter  = new TagAdapter<String>(expenditureType){
            @Override
            public View getView(FlowLayout parent, int position, String s){
                TextView tv = (TextView) mInflater.inflate(R.layout.item_tv_flow,tfl_income, false);
                tv.setText(s);
                return tv;
            }
        });

        List<String> transactionType = new ArrayList<>();

        SimpleDateFormat df = new SimpleDateFormat("yyyy.MM");//设置日期格式
        SimpleDateFormat dfResult = new SimpleDateFormat("yyyyMM");//设置日期格式
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());

        for(int i=0;i<6;i++){

            Date m = c.getTime();
            transactionType.add(df.format(m));

            c.add(Calendar.MONTH, -1);
            transactionTypeResult.add(dfResult.format(m));
        }

        tfl_transactionTime.setAdapter(transactionAdapter = new TagAdapter<String>(transactionType){
            @Override
            public View getView(FlowLayout parent, int position, String s){
                TextView tv = (TextView) mInflater.inflate(R.layout.item_tv_flow,tfl_income, false);
                tv.setText(s);
                return tv;
            }
        });

        tfl_income.setOnTagClickListener((view, position, parent) -> {
            switch (position){
                case 0:
                    billFilterBean.setInType("0");
                    break;
                case 1:
                    billFilterBean.setInType("3");
                    break;
                case 2:
                    billFilterBean.setInType("2");
                    break;
                case 3:
                    billFilterBean.setInType("4");
                    break;
                    default:
                        billFilterBean.setInType("");
                        break;
            }
            return true;
        });

        tfl_expenditure.setOnTagClickListener((view, position, parent) -> {
            switch (position){
                case 0:
                    billFilterBean.setOutType("0");
                    break;
                case 1:
                    billFilterBean.setOutType("1");
                    break;
                case 2:
                    billFilterBean.setOutType("2");
                    break;
                default:
                    billFilterBean.setOutType("");
                    break;
            }
            return true;
        });
        tfl_transactionTime.setOnTagClickListener((view, position, parent) -> {
            if (position!=-1) {
                billFilterBean.setTradeDate(transactionTypeResult.get(position));
            }else{
                billFilterBean.setTradeDate("");
            }

            return true;
        });

    }

    BillFilterBean billFilterBean = new BillFilterBean();
}
